
import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/router/routes.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_product_category_model.dart';
import 'package:fzcgx_app/ui/my/factory/page/select_factory_products2_page.dart';

class SelectFactoryProducts1ViewModel extends BaseViewModel {
  List<FactoryProductCategoryModel> models =
      List<FactoryProductCategoryModel>();
  SelectFactoryProducts1ViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.postList<FactoryProductCategoryModel>(
        'pro-cate/parent-list', null, onSuccess: (data) {
      if (data != null) {
        this.models = data;

        this.setState(ViewState.Success);
      } else {
        this.setState(ViewState.Empty);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void onSelectChip(FactoryProductCategoryModel model) {
    models.forEach((element) {
      if(element != model){
 element.check = false;
      }
     
    });
    model.check = !model.check;
    notifyListeners();
  }

  void onNextStep(BuildContext context) {
    FactoryProductCategoryModel factoryProductCategoryModel =
        getSelectFactoryProductCategoryModel();
        print(factoryProductCategoryModel);
       if(null == factoryProductCategoryModel){
          FLToast.showText(text: '请选择，最多选择一个');
         return;
       }
    Routes.navigateTo(
        context,
        SelectFactoryProducts2Page(
          pId: factoryProductCategoryModel.id,
        ));
  }

  FactoryProductCategoryModel getSelectFactoryProductCategoryModel() {
    FactoryProductCategoryModel model;
    // ignore: missing_return
    models.forEach((element) {
      if (element.check) {
        model= element;
      }
    });
    return model;
  }
}
